<template>
  <div class="mock-test">
    <h2>Mock接口测试</h2>

    <!-- 登录测试 -->
    <el-card style="margin: 20px 0;">
      <template #header>
        <span>登录接口测试</span>
      </template>
      <el-form>
        <el-form-item label="用户名:">
          <el-input v-model="loginForm.adminname" placeholder="请输入用户名(测试: admin)" />
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码(测试: 123456)" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loginLoading">
            登录测试
          </el-button>
        </el-form-item>
      </el-form>
      <div v-if="loginResult" style="margin-top: 10px;">
        <h4>登录结果:</h4>
        <pre>{{ JSON.stringify(loginResult, null, 2) }}</pre>
      </div>
    </el-card>

    <!-- 用户列表测试 -->
    <el-card style="margin: 20px 0;">
      <template #header>
        <span>用户列表接口测试</span>
      </template>
      <el-button type="primary" @click="handleGetUserList" :loading="userListLoading">
        获取用户列表
      </el-button>
      <div v-if="userList.length > 0" style="margin-top: 15px;">
        <el-table :data="userList" style="width: 100%">
          <el-table-column prop="id" label="ID" width="100" />
          <el-table-column prop="adminname" label="用户名" width="120" />
          <el-table-column prop="name" label="姓名" width="100" />
          <el-table-column prop="email" label="邮箱" width="180" />
          <el-table-column prop="role" label="角色" width="100" />
          <el-table-column prop="status" label="状态" width="100" />
          <el-table-column prop="createTime" label="创建时间" width="180" />
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script>
import { login, getUserList } from '@/api/user'

export default {
  name: 'MockTest',
  data() {
    return {
      loginForm: {
        adminname: 'admin',
        password: '123456'
      },
      loginResult: null,
      loginLoading: false,
      userList: [],
      userListLoading: false
    }
  },
  methods: {
    async handleLogin() {
      this.loginLoading = true
      try {
        const result = await login(this.loginForm.adminname, this.loginForm.password)
        this.loginResult = result
        this.$message.success('登录成功！')
      } catch (error) {
        this.$message.error('登录失败: ' + error.message)
        console.error('Login error:', error)
      } finally {
        this.loginLoading = false
      }
    },

    async handleGetUserList() {
      this.userListLoading = true
      try {
        const result = await getUserList()
        this.userList = result.data.list
        this.$message.success('获取用户列表成功！')
      } catch (error) {
        this.$message.error('获取用户列表失败: ' + error.message)
        console.error('Get user list error:', error)
      } finally {
        this.userListLoading = false
      }
    }
  }
}
</script>

<style scoped>
.mock-test {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

pre {
  background: #f5f5f5;
  padding: 15px;
  border-radius: 4px;
  overflow-x: auto;
}
</style>
